package com.suning.sawp.service.impl.assistant;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.suning.sawp.intf.assistant.AssistantDaoService;
import com.suning.sawp.po.assistant.Assistant;

@Service
public class AssistantDaoServiceImpl implements AssistantDaoService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AssistantDaoServiceImpl.class);
    
    @Resource
    MongoTemplate mongoTemplate;
    
    @Override
    public void addAssistat(Assistant assistant) {
        Preconditions.checkNotNull(assistant);
        if(StringUtils.isNotBlank(assistant.getId())){
            LOGGER.error("addAssistat : assistant's id is empty");
            throw Throwables.propagate(new Exception("addAssistat : assistant's id is empty"));
        }
        assistant.setCreateTime(new Date());
        assistant.setUpdateTime(new Date());
        mongoTemplate.insert(assistant);
    }

    @Override
    public void updateAssistat(Assistant assistant) {
        Preconditions.checkNotNull(assistant);
        Preconditions.checkNotNull(assistant.getId());
        assistant.setUpdateTime(new Date());
        mongoTemplate.save(assistant);
    }

    @Override
    public void removeAssistat(String id) {
        Preconditions.checkNotNull(id);
        Assistant assistant = new Assistant();
        assistant.setId(id);
        mongoTemplate.remove(assistant);
    }

    @Override
    public List<Assistant> queryAllAssistat() {
        return mongoTemplate.findAll(Assistant.class);
    }

    @Override
    public Assistant queryAssistatByStaffId(String staffId) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("staffId").is(staffId);
        query.addCriteria(criteria);
        return mongoTemplate.findOne(query, Assistant.class);
    }

}
